package com.yuven.appframework.manager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import com.yuven.appframework.manager.listener.INetWorkConnectListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class NetWorkManager extends BroadcastReceiver {
    private static NetWorkManager mInstance;
    public ArrayList<WeakReference<INetWorkConnectListener>> mListeners = new ArrayList<>();

    public static NetWorkManager getInstance() {
        if (mInstance == null) {
            synchronized (NetWorkManager.class) {
                if (mInstance == null) {
                    mInstance = new NetWorkManager();
                }
            }
        }
        return mInstance;
    }

    public void init(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(this, filter);
    }

    public void registerListener(INetWorkConnectListener listener) {
        synchronized (mListeners) {
            WeakReference<INetWorkConnectListener> listenerWef = new WeakReference<>(listener);
            mListeners.add(listenerWef);
        }
    }

    public void unRegisterListener(INetWorkConnectListener iNetWorkConnectListener) {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); i++) {
                WeakReference<INetWorkConnectListener> listenerRef = mListeners.get(i);
                INetWorkConnectListener listener = listenerRef.get();
                if (listener == iNetWorkConnectListener) {
                    mListeners.remove(i);
                }
            }
        }
    }

    private void OnDisconNet() {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); i++) {
                WeakReference<INetWorkConnectListener> listenerRef = mListeners.get(i);
                INetWorkConnectListener listener = listenerRef.get();
                if (listener != null) {
                    listener.onDisconnect();
                }
            }
        }
    }

    private void OnConMobileNet() {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); i++) {
                WeakReference<INetWorkConnectListener> listenerRef = mListeners.get(i);
                INetWorkConnectListener listener = listenerRef.get();
                if (listener != null) {
                    listener.onConMobileNet();
                }
            }
        }
    }

    private void OnConWifiNet() {
        synchronized (mListeners) {
            for (int i = 0; i < mListeners.size(); i++) {
                WeakReference<INetWorkConnectListener> listenerRef = mListeners.get(i);
                INetWorkConnectListener listener = listenerRef.get();
                if (listener != null) {
                    listener.onConWifiNet();
                }
            }
        }
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager == null) {
                // return "当前网络已断开";
                OnDisconNet();
                return;
            }
            //获取网络连接信息
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()) {
                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    //return "当前使用WIFI网络";
                    OnConWifiNet();
                    return;
                }
                if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    //   return "当前使用移动数据网络";
                    OnConMobileNet();
                    return;
                }
                return;
            }
            OnDisconNet();
        }

    }

    private static NetworkInfo getNetworkInfo(Context context) {
        return ((ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    }

    public static boolean isNetworkAvailable(Context context) {
        NetworkInfo networkInfo = getNetworkInfo(context);
        if (networkInfo == null)
            return false;
        return (networkInfo.isConnected() && networkInfo.isAvailable());
    }
}
